package com.libservice

import android.net.Uri
import com.libui.dialog.DialogCompletion
import com.libui.toast.ToastCompletion

enum class RouteResult(val value: Int) {
    // push success
    SUCCESS(0),

    // push error
    ERROR(1),

    // redirect to login
    REDIRECTTOLOGIN(2);

    companion object {
        @Suppress("unused")
        fun getByValue(value: Int) = values().firstOrNull { it.value == value }

        fun getByResult(result: Boolean) = if (result) SUCCESS else ERROR
    }

    val isSuccess: Boolean
        get() = this == SUCCESS
}

typealias RouteCompletion = (result: RouteResult, param: Any?) -> Unit

interface RouterServiceInterface {
    /**
     * 路由栈url列表
     */
    val urls: List<String>

    fun openDeepLink(url: String, params: Map<String, Any>?, completion: RouteCompletion?)

    fun open(url: String, params: Map<String, Any>?, completion: RouteCompletion?)

    fun push(url: String, animated: Boolean)

    fun push(
        url: String, params: Map<String, Any>?, animated: Boolean, completion: RouteCompletion?
    )

    fun push(
        uri: Uri, params: Map<String, Any>?, animated: Boolean, completion: RouteCompletion?
    )

    fun pop(animated: Boolean): Boolean

    fun pop(value: Any?, animated: Boolean): Boolean

    fun present(url: String, animated: Boolean)

    fun present(
        url: String, params: Map<String, Any>?, animated: Boolean, completion: RouteCompletion?
    )

    fun present(
        uri: Uri, params: Map<String, Any>?, animated: Boolean, completion: RouteCompletion?
    )

    fun dismiss(animated: Boolean): Boolean

    fun dismiss(value: Any?, animated: Boolean): Boolean

    /**
     * 关闭指定页面
     *
     * @param urls 页面列表
     */
    fun finish(urls: List<String>)

    /**
     * 关闭当前页面，返回上一页面或多级页面
     *
     * @param delta 返回的页面数，如果 delta 大于现有页面数，则返回到首页。
     * @param value 返回结果
     * @param animated 支持返回动效
     *
     * @return result
     */
    fun pop(delta: Int, value: Any?, animated: Boolean): Boolean

    fun handle(
        url: String, params: Map<String, Any>?, completion: RouteCompletion?
    )

    fun handle(
        uri: Uri, params: Map<String, Any>?, completion: RouteCompletion?
    )

    fun handleNative(
        url: String, params: Map<String, Any>?, completion: RouteCompletion?
    )

    fun handleNative(
        uri: Uri, params: Map<String, Any>?, completion: RouteCompletion?
    )

    fun alert(
        dialogId: String?,
        title: String?,
        message: String,
        confirmButtonText: String?,
        interactive: Boolean,
        completion: DialogCompletion?
    )

    fun alert(
        title: String?,
        message: String,
    )

    fun confirm(
        dialogId: String?,
        title: String?,
        message: String,
        confirmButtonText: String?,
        cancelButtonText: String?,
        interactive: Boolean,
        completion: DialogCompletion?
    )

    fun toast(message: String, interactive: Boolean, completion: ToastCompletion?)

    fun loading(message: String, interactive: Boolean, completion: ToastCompletion?)

    fun closeToast()
}